<?php

class Scrollerable extends CWidget {
	
	public $id;
	public $step;
	public $interval;
	public $direction;
	public $wrapper;
	public $items;
	public $autoPlay;
	public $afterScroll;
	public $beforeScroll;
	public $scrollInterval;
	public $blockSize;
	
	private static $_counter = 0;
	
	public function run() {
		self::registerScript();
		$id = $this->id ? $this->id : 'scrollerable_'.(++self::$_counter);
		$options = array(
			'id' => $id,
			'wrapper' => $this->wrapper,
			'items' => $this->items,
			'direction' => $this->direction,
			'interval' => $this->interval,
			'step' => $this->step,
			'autoPlay' => $this->autoPlay,
			'scrollInterval' => $this->scrollInterval,
			'afterScroll' => 'js:'.$this->afterScroll,
			'beforeScroll' => 'js:'.$this->beforeScroll,
			'blockSize' => $this->blockSize,
		);
		
		$newOptions = array();
		foreach($options as $k=>$v) {
			if($v === null || $v=='js:') continue;
			$newOptions[$k] = $v;
		}
		
		$newOptions = CJavaScript::encode($newOptions);
		
		$script = "$.scrollerable({$newOptions})";
		Yii::app()->clientScript->registerScript(
			$id,
			$script, 
			CClientScript::POS_END
		);
	}
	
	public static function registerScript() {
		$cs = Yii::app()->clientScript;
		$am = Yii::app()->assetManager;
		$jsUrl = $am->publish(dirname(__FILE__).'/js/jquery.scrollerable.js');
		$cs->registerScriptFile($jsUrl);
	}

}

?>